//
//  FXCalcAppDelegate.m
//  FXCalc
//
//  Created by jiro on 12/06/24.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "FXCalcAppDelegate.h"

@implementation FXCalcAppDelegate

@synthesize window;
@synthesize entryExchangeRate;
@synthesize targetDelta;
@synthesize lossCutDelta;
@synthesize charge;
@synthesize spread;
@synthesize investMoney;
@synthesize targetInterestRate;
@synthesize breakEvenWinRate;
@synthesize targetWinnigExchangeCount;
@synthesize targetMinimumExchangeCount;
@synthesize targetMaxmumExchangeCount;
@synthesize totalExchangeCount;
@synthesize evenWinCount;
@synthesize minimumTargetDelta;
@synthesize profitAtTargetRate;
@synthesize profitAtLossCutRate;
@synthesize profitPercentageAtTargetRate;
@synthesize profitPercentageAtLossCutRate;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
	// Insert code here to initialize your application 

	NSDate* date = [ NSDate dateWithString: @"1978-5-10 00:00:00 +0900" ];
	NSLog( @"%@", date );

}

- (void) calc: (id) sender
{
	double breakPointExchangeRate = 0;
	breakPointExchangeRate = ( [ lossCutDelta doubleValue ] + 2 * [ spread doubleValue ] ) / ( [ targetDelta doubleValue ] + [ lossCutDelta doubleValue ] );
	breakPointExchangeRate += - ( 2 * [ charge doubleValue ] * ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) ) /
								( [ investMoney doubleValue ] * ( [ targetDelta doubleValue ] + [ lossCutDelta doubleValue ] ) );
	double winCount = 0;
	double minCount = 0;
	double maxCount = 0;
	double count = 0;
	

	double dX = - [ targetDelta doubleValue ] + 2 * [ spread doubleValue ] - 2 * [ charge doubleValue ] * ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) ;
	double dL = [ lossCutDelta doubleValue ] + 2 * [ spread doubleValue ] - 2 * [ charge doubleValue ] * ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] );

	count = - [ targetInterestRate doubleValue ] * ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) / dX;
	if ( dX > 0 )
	{
		minCount = count;
	}
	if ( dX < 0 )
	{
		maxCount = count;
	}

	count = - [ targetInterestRate doubleValue ] * ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) / dL;
	if ( dL > 0 )
	{
		if ( count > minCount )
		{
			minCount = count;
		}
	}
	if ( dL < 0 )
	{
		if ( count < maxCount )
		{
			maxCount = count;
		}
	}

	winCount = ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) / ( [ targetDelta doubleValue ] + [ lossCutDelta doubleValue ] );
	winCount *= [ targetInterestRate doubleValue ] -
				[ totalExchangeCount doubleValue ] *
					( ( - [ lossCutDelta doubleValue ] - 2 * [ spread doubleValue ] ) / 
						( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] )
					- 2 * [ charge doubleValue ] );

	[ breakEvenWinRate setDoubleValue: breakPointExchangeRate ];
	[ targetWinnigExchangeCount setDoubleValue: winCount ];
	[ evenWinCount setDoubleValue: breakPointExchangeRate * [ totalExchangeCount doubleValue ] ];
	[ targetMinimumExchangeCount setDoubleValue: minCount ];
	[ targetMaxmumExchangeCount setDoubleValue: maxCount ];
	[ minimumTargetDelta setDoubleValue: 2 * ( [ charge doubleValue ] * ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) / [ investMoney doubleValue ] + [ spread doubleValue ] ) ];

	[ profitAtTargetRate setDoubleValue: [ investMoney doubleValue ] * ( [ targetDelta doubleValue ] - 2 * [ spread doubleValue ] ) / ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) - 2 * [ charge doubleValue ] ];
	[ profitAtLossCutRate setDoubleValue: [ investMoney doubleValue ] * ( - [ lossCutDelta doubleValue ] - 2 * [ spread doubleValue ] ) / ( [ entryExchangeRate doubleValue ] + [ spread doubleValue ] ) - 2 * [ charge doubleValue ] ];
	[ profitPercentageAtTargetRate setDoubleValue: 100 * [ profitAtTargetRate doubleValue ] / [ investMoney doubleValue ] ];
	[ profitPercentageAtLossCutRate setDoubleValue: 100 * [ profitAtLossCutRate doubleValue ] / [ investMoney doubleValue ] ];
}

@end
